package edu.gznu.ics.fast.pulsar.service.prestoacc.picService.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import edu.gznu.ics.fast.pulsar.model.vo.prestoacc.task.computeTask.TaskMonitorDetailHeaderVO;
import edu.gznu.ics.fast.pulsar.model.vo.prestoacc.task.computeTask.TaskMonitorDetailItemVO;
import edu.gznu.ics.fast.pulsar.service.prestoacc.picService.TaskMonitorDetailService;

@Service("tskMonitorDetailService")
public class TaskMonitorDetailServiceImpl implements TaskMonitorDetailService {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public TaskMonitorDetailHeaderVO findHeader(int fileId, int taskId) {
		String sql = "select t.file_id, p.file_name, t.task_id, t.startdt, t.enddt, "
				+ "p.telescope, p.date_obs, p.dec_str, p.dec2000, p.lo_freq, hi_freq "
				+ "from t_task t "
				+ "left join t_fits_info p "
				+ "on t.file_id = p.file_id "
				+ "where t.file_id = ? and t.task_id = ? ";
		Map<String, Object> result = jdbcTemplate.queryForMap(sql, fileId, taskId);
		TaskMonitorDetailHeaderVO header = new TaskMonitorDetailHeaderVO();
		header.setFileId(((BigDecimal) result.get("FILE_ID")).intValue());
		header.setFileName((String) result.get("FILE_NAME"));
		header.setTaskId(((BigDecimal) result.get("TASK_ID")).intValue());
		header.setStartTime((String) result.get("STARTDT"));
		header.setEndTime((String) result.get("ENDDT"));
		header.setTelescope((String) result.get("TELESCOPE"));
		header.setDateObs((String) result.get("DATEOBS"));
		header.setDecStr((String) result.get("DEC_STR"));
		header.setDec2000(((BigDecimal) result.get("DEC2000")).floatValue());
		header.setLoFreq(((BigDecimal) result.get("LO_FREQ")).floatValue());
		header.setHiFreq(((BigDecimal) result.get("HI_FREQ")).floatValue());
		return header;
	}

	@Override
	public List<TaskMonitorDetailItemVO> findListByFileIdAndTaskId(int fileId, int taskId) {
		String sql = "select t.dm, t.period,  p.file_name "
				+ "from t_pic_info t "
				+ "left join t_fits_info p "
				+ "on t.file_id = p.file_id "
				+ "where t.file_id = ? and t.task_id = ? ";
		List<TaskMonitorDetailItemVO> itemList = new LinkedList<TaskMonitorDetailItemVO>();
		List<Map<String, Object>> results = jdbcTemplate.queryForList(sql, fileId, taskId);
		for(int i = 0; i < results.size(); i++) {
			Map<String, Object> row = results.get(i);
			TaskMonitorDetailItemVO vo = new TaskMonitorDetailItemVO();
			vo.setNumber(i + 1);
			vo.setDm((BigDecimal) row.get("DM"));
			vo.setPeriod((BigDecimal) row.get("PERIOD"));
			vo.setFlag(((BigDecimal) row.get("FLAG")).byteValue());
			String fitsFileName = (String) row.get("FILE_NAME");
			String[] splits = StringUtils.split(fitsFileName, ".");
			fitsFileName = splits[0];
			splits = StringUtils.split(fitsFileName, "_");
			String fits1 = splits[0];
			String fits2 = splits[1];
			DecimalFormat twoBitDecimalFormat = new DecimalFormat("#.00");
			String dmString = twoBitDecimalFormat.format(
					vo.getDm().doubleValue());
			String periodString = twoBitDecimalFormat.format(
					vo.getPeriod().doubleValue());
			String pictureName = fits2 + "_" 
					+ "DM" + dmString + "_" 
					+ periodString + "ms" + "_"
					+ "Cand.pfd.png";
			vo.setPictureName(pictureName);
			String picturePath = "/output/1/picture/" + fits1 + "/" + fitsFileName + "/" + pictureName;
			vo.setPicturePath(picturePath);
			itemList.add(vo);
		}
		return itemList;
	}
}
